<template>
  <div>
    <div class="top">
      <img src="../../images/logo.png" width="64px" height="64px" alt="">
      <span>
        认证机构信息管理系统
      </span>
    </div>
    <div class="middle">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
        label-position="left">
        <el-input ref="username" prefix-icon="el-icon-user" style="width: 19%; position: absolute; top: 28%;right: 20%;"
          v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" autocomplete="on">
        </el-input>
        <el-input ref="password" prefix-icon="el-icon-lock" style="width: 19%; position: absolute; top: 51%;right: 20%;"
          v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" name="password" tabindex="2"
          autocomplete="on" @keyup.native="checkCapslock" @blur="capsTooltip = false" @keyup.enter.native="handleLogin">
        </el-input>
        <el-button :loading="loading" type="primary" style="width: 19%; position: absolute; top: 70%;right: 20%;"
          @click.native.prevent="handleLogin">登录
        </el-button>
      </el-form>
    </div>
    <div class="bottom">
      <span>Copyright@湖南省环通企业管理咨询有限公司</span><span>|</span><span><a
          href="https://beian.miit.gov.cn/">湘ICP备2020016720号-2</a></span>
    </div>
  </div>
</template>

<script>
import {
  validUsername
} from '@/utils/validate'
import SocialSign from './components/SocialSignin'
import RequestObject from '@/utils/requestObject'
import {
  constants
} from 'crypto';

export default {
  name: 'Login',
  components: {
    SocialSign
  },
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{
          required: true,
          trigger: 'blur',
          message: '请输入账号'
        }],
        password: [{
          required: true,
          trigger: 'blur',
          message: '请输入密码'
        }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created () {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted () {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed () {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock ({
      shiftKey,
      key
    } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          var postData = {
            loginName: this.loginForm.username,
            passwd: this.loginForm.password
          }
          var reqObject = RequestObject.CreatePostObject(postData)
          this.$store.dispatch('user/login', reqObject)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
              
            })
            .catch((error) => {             
              this.$message(error)
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery (query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

.el-button--small {
  padding: 16px 15px;
  font-size: 22px;
}

//.el-input >>> .el-input__inner
//.el-input--small .el-input__inner {
.el-input >>> .el-input__inner {
  height: 55px;
  line-height: 55px;
}

div {
  margin: 0;
  padding: 0;
}

.top {
  position: absolute;
  top: 0;
  width: 100%;
  height: 20%;
}

.top img {
  position: relative;
  top: 60%;
  left: 15%;
}

.top span {
  position: relative;
  top: 49%;
  left: 16%;
  font-size: 2.4em;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 500;
  color: dimgrey;
}

.middle {
  position: absolute;
  top: 20%;
  width: 100%;
  height: 55%;
  background: url(../../assets/images/bg2.jpg) no-repeat;
  background-size: 100% 100%;
}

.bottom {
  position: absolute;
  top: 75%;
  width: 100%;
  height: 25%;
}

.bottom span {
  position: relative;
  top: 80%;
  left: 38%;
  font-size: 16px;
  color: dimgrey;
}

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
